"VERSION 1
" 在行开始加注释
"===============================================================
" " 注释python代码的当前行
" " 1.#Comment
" function! g:commenter#Comment()
"     "2. '.' 表示当前行
"     let l:line = getline('.')
"     call setline('.', '# ' . l:line)
" endfunction

" "3. :help g g是一个可供用户使用的自由命名空间
" " c 表示注释comment
" nnoremap gc :call g:commenter#Comment()<cr>

"VERSION 2
" 注释缩进
"===============================================================
" " 注释python代码的当前行
" function! g:commenter#Comment()
"     let l:i = indent('.') " 缩进的空格数目
"     let l:line = getline('.')
"     call setline('.', l:line[:l:i-1] . '# ' . l:line[l:i:])
" endfunction

" nnoremap gc :call g:commenter#Comment()<cr>


"VERSION 3
" 光标位置修正
"===============================================================
" let s:comment_string = '# '

" function! g:Comment()
"     let l:i = indent('.')
"     let l:line = getline('.')
"     let l:cur_row = getcurpos()[1]
"     let l:cur_col = getcurpos()[2]
"     call setline('.', l:line[:l:i-1] . s:comment_string . l:line[l:i:])
"     call cursor(l:cur_row, l:cur_col + len(s:comment_string))
" endfunction

" nnoremap gc :call g:Comment()<cr>

"VERSION 4
" 注释切换
"===============================================================
" let s:comment_string = '# '

" function! g:ToggleComment()
"     let l:i = indent('.')
"     let l:line = getline('.')
"     let l:cur_row =getcurpos()[1]
"     let l:cur_col = getcurpos()[2]

"     if l:line[l:i:l:i+len(s:comment_string)-1] == s:comment_string
"         call setline('.', l:line[:i-1] .
"         \ l:line[l:i + len(s:comment_string):])
"         let l:cur_offset = -len(s:comment_string)
"     else
"         call setline('.', l:line[:l:i-1] . s:comment_string . l:line[l:i:])
"         let l:cur_offset = len(s:comment_string)
"     endif
"     call cursor(l:cur_row, l:cur_col + l:cur_offset)
" endfunction

" nnoremap gc :call g:ToggleComment()<cr>

"VERSION 5
" 处理没有缩进的行
"===============================================================
" let s:comment_string = '# '

" function! g:ToggleComment()
"     let l:i = indent('.')
"     let l:line = getline('.')
"     let l:cur_row =getcurpos()[1]
"     let l:cur_col = getcurpos()[2]
"     let l:prefix = l:i > 0 ? l:line[:l:i-1] : ''

"     if l:line[l:i:l:i+len(s:comment_string)-1] == s:comment_string
"         call setline('.', l:prefix . l:line[l:i + len(s:comment_string):])
"         let l:cur_offset = -len(s:comment_string)
"     else
"         call setline('.', l:prefix . s:comment_string . l:line[l:i:])
"         let l:cur_offset = len(s:comment_string)
"     endif
"     call cursor(l:cur_row, l:cur_col + l:cur_offset)
" endfunction

" nnoremap gc :call g:ToggleComment()<cr>

"VERSION 6
" 整理插件布局
"===============================================================
" nnoremap gc :call g:commenter#ToggleComment()<cr>
" " 加载流程:
" "   1.打开vim,加载plugin/commenter.vim
" "   2.打开py文件,加载ftplugin/python.vim
" "   3.执行gc,加载autoload/commenter.vim

"VERSION 7
" 多行注释
" v:count - 访问映射之前的数字
" v:count1 - 默认为1
"===============================================================
nnoremap gc :<c-u>call g:commenter#ToggleComment(v:count1)<cr>
"<c-u>是使用v:count或v:count1的前提条件.
" :help v:count
" :help v:count1

" 可视模式的多行选择实现多行注释
vnoremap gc :<c-u>call g:commenter#ToggleComment(line("'>") - line("'<") + 1)<cr>
" line("'>") - 获得选择的最后一行的行号
" line("'<") - 获得选择的第一行的行号